﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlashCards.Model;

namespace FlashCards.Tests.NeuralNetworkTest.Simulations
{
    public class LearningSetsPreparator
    {
        public List<double[]> InputSets { get; set; }
        public List<Dictionary<int, double>> InputDictionarySets { get; set; }
        public List<double[]> PatternSet { get; set; }

        public LearningSetsPreparator(int flashcardsCount, int responseCount)
        {
            InputSets = new List<double[]>();
            PatternSet = new List<double[]>();

            for (int i = 0; i < flashcardsCount; i++)
            {
                InputSets.Add(new double[flashcardsCount]);
                InputSets[i][i] = 1;

                PatternSet.Add(new double[responseCount]);
                PatternSet[i][0] = 1;
            }
        }

        public LearningSetsPreparator(Repeat repeat)
        {
            InputSets = new List<double[]>();
            PatternSet = new List<double[]>();

            InputDictionarySets = new List<Dictionary<int, double>>();

            //var flashcards = repeat.FlashcardSet.Flashcards.ToList();

            var flashcards = repeat.RepeatItems.Select(x => x.Flashcard).ToList();
            
            for (int i = 0; i < flashcards.Count; i++)
            {
                InputSets.Add(new double[flashcards.Count]);
                InputSets[i][i] = 1;

                var dict = new Dictionary<int, double>();
                dict.Add(flashcards[i].FlashcardId, 1);
                for (int j = 0; j < flashcards.Count; j++)
                {
                    if (j == i)
                        continue;
                    dict.Add(flashcards[j].FlashcardId, 0);
                }
                InputDictionarySets.Add(dict);
            
                var index = repeat.RepeatItems.FirstOrDefault(x => x.FlashcardId == flashcards[i].FlashcardId).RepeatMark;

                PatternSet.Add(new double[4]);
                PatternSet[i][index] = 1;
            }
        }

        public void SimulateUserProgress(int flashcardNumber)
        {
            var table = PatternSet[flashcardNumber];

            int index = 0;
            for (int i = 0; i < table.Length; i++)
            {
                if (table[i] == 1 && i < table.Length - 1)
                {
                    index = i;
                    table[i] = 0;
                    break;
                }
            }

            table[index + 1] = 1;
        }

        public void WritePatternSets()
        {
            foreach (var item in PatternSet)
            {
                foreach (var num in item)
                {
                    Console.Write(num + "  ");
                }
                Console.WriteLine();
            }
        }
    }
}
